package com.corner;

import com.alibaba.fastjson2.JSON;
import com.corner.system.domain.CtrMsg;
import com.corner.system.mqtt.MqttSendClient;
import com.corner.system.domain.Db;
import com.corner.system.domain.MqttReq;
import com.corner.system.mapper.SysJxCommonDataMapper;
import com.corner.system.mapper.SysPlcMapper;
import com.corner.system.service.JxMqttService;

import com.corner.system.websocket.WebSocketClientHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ruoyi
 * @description: ceshi
 * @author: lydms
 * @create: 2024-03-25 ,16:11
 **/
@SpringBootTest(classes= RuoYiApplication.class)
@RunWith(SpringRunner.class)
public class test1 {
    @Autowired
    private MqttSendClient mqttSendClient;
    @Autowired
    private SysPlcMapper plcMapper;
    @Autowired
    private JxMqttService jxMqttService;
    @Autowired
    private SysJxCommonDataMapper commonDataMapper;

    @Autowired
    private WebSocketClientHandler webSocketClientHandler;


    @Test
    public void mqtest() throws NoSuchFieldException, IllegalAccessException {
        List<Long> stationIds = commonDataMapper.selectStationIds();
        for(Long i:stationIds){
            MqttReq mqttReq = jxMqttService.selectWswDataList(i);
            if(mqttReq == null){
                return;
            }
            Map data = mqttReq.getData();
            String content = JSON.toJSONString(data);
            try {
                FileWriter fileWriter = new FileWriter("C:/Users/chenhenan/Desktop/mqtt/"+i+".txt");
                fileWriter.write(content);
                fileWriter.close();
                System.out.println("内容已成功写入文件");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public static void main(String[] args) throws Exception{
        CtrMsg ctrMsg = new CtrMsg();
        ctrMsg.setMsgId("1111");
        String s = ctrMsg.toString();
        System.out.println(s);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(ctrMsg);
        System.out.println(json);
    }

    @Value("${mqtt.defaultTopic}")
    private String defaultTopic;
    @Test
    public void testtopicdym() throws Exception {
        webSocketClientHandler.connect();

    }


}
